package com.itqf.service.impl;

import com.itqf.mapper.DtsCategoryMapper;
import com.itqf.mapper.DtsOrderMapper;
import com.itqf.mapper.DtsUserMapper;
import com.itqf.service.DashboardService;
import com.itqf.vo.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;

/**
 * projectName: store_parent
 *
 * @author: 赵伟风
 * time: 2021/6/9 15:03
 * description:
 */
@Service
public class DashboardServiceImpl implements DashboardService {


    Logger logger = LoggerFactory.getLogger(DashboardServiceImpl.class);

    @Autowired
    private DtsUserMapper dtsUserMapper;
    @Autowired
    private DtsOrderMapper dtsOrderMapper;
    @Autowired
    private DtsCategoryMapper dtsCategoryMapper;


//    public static void main(String[] args) {
//
//
//        Integer [] ids = {1,3,4};
//        Integer [] oid = {1,3,5};
//
//        Set<Integer> idsSet = new HashSet<Integer>();
//        Set<Integer> idsSets = new HashSet<Integer>();
//
//        //提取交集的方法！
//        idsSet.retainAll(idsSets);
//
//    }

    /**
     * 首页土拍你数据封装业务！
     * @param day 查询的天数
     * @return
     */
    @Override
    public Map<String, Object> ShowChart(int day) {

        //1.数据库数据查询
        // [{dayStr:2021-01-01,cnts:10},{dayStr:2021-01-04,cnts:10},{dayStr:2021-01-06,cnts:10}]
        List<DayStatis> userDayStatisList =  dtsUserMapper.queryUserStatis(day);
         //[{dayStr:2021-01-01,cnts:10,amts:998},{dayStr:2021-01-03,cnts:10,amts:98},{dayStr:2021-01-07,cnts:10}]
        List<DayStatis> orderDayStatisList=  dtsOrderMapper.queryOrderStatis(day);

        List<CategorySellAmts> categorySellAmtsList = dtsCategoryMapper.queryCategoryStatis();
        //2.封装数据格式

        //用户和订单显示数据
        Set<String> times = new HashSet<>();
        for (DayStatis dayStatis : userDayStatisList) {
            //用户的时间装入
            times.add(dayStatis.getDayStr());
        }
        for (DayStatis dayStatis : orderDayStatisList) {
            times.add(dayStatis.getDayStr());
        }

        //用户的增长量
        int [] userUp = new int[times.size()];
        int index = 0;
        for (String time : times) {

            int cnts = 0;
            for (DayStatis dayStatis : userDayStatisList) {
                if (dayStatis.getDayStr().equalsIgnoreCase(time))
                {
                    cnts = dayStatis.getCnts();
                    break;
                }
            }

            userUp[index]=cnts;
            index++;
        }

        //订单的增长量
        int [] orderUp = new int[times.size()];
        index = 0;
        for (String time : times) {

            int cnts = 0;
            for (DayStatis dayStatis : orderDayStatisList) {
                if (dayStatis.getDayStr().equalsIgnoreCase(time))
                {
                    cnts = dayStatis.getCnts();
                    break;
                }
            }

            orderUp[index]=cnts;
            index++;
        }

        //时间转成数组
        String[] timeArray = times.toArray(new String[0]);

        UserOrderCntVo userOrderCntVo = new UserOrderCntVo();
        userOrderCntVo.setDayData(timeArray);
        userOrderCntVo.setOrderCnt(orderUp);
        userOrderCntVo.setUserCnt(userUp);

        //订单的vo
        String [] orderTimeArray =  new String[orderDayStatisList.size()];
        BigDecimal [] orderAmtData = new BigDecimal[orderDayStatisList.size()];
        int [] orderCntData = new int[orderDayStatisList.size()];

        for (int i = 0; i < orderDayStatisList.size() ;i++ ) {
            orderTimeArray[i] = orderDayStatisList.get(i).getDayStr();
            orderAmtData[i] = orderDayStatisList.get(i).getAmts();
            orderCntData[i] = orderDayStatisList.get(i).getCnts();
        }

        OrderAmtsVo orderAmtsVo = new OrderAmtsVo();
        orderAmtsVo.setDayData(orderTimeArray);
        orderAmtsVo.setOrderAmtData(orderAmtData);
        orderAmtsVo.setOrderCntData(orderCntData);

        //类别和销售额
        CategorySellVo sellVo = new CategorySellVo();


        String [] categoryNames = new String[categorySellAmtsList.size()];

        for (int i = 0; i < categorySellAmtsList.size(); i++) {
            categoryNames[i] = categorySellAmtsList.get(i).getName();
        }
        sellVo.setCategorySellData(categorySellAmtsList);
        sellVo.setCategoryNames(categoryNames);


        //3.统一打包返回
        Map<String,Object> data = new HashMap<>();
        data.put("categorySell", sellVo);
        data.put("orderAmts", orderAmtsVo);
        data.put("userOrderCnt", userOrderCntVo);


        return data;
    }
}
